﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharedLib.WinFormMVCFramework;
using Tracy.DataModel.Config;
using Tracy.Plugins.DownloadAdapters.View;
using System.Windows.Forms;

namespace Tracy.Plugins.DownloadAdapters
{
    class UTorrentConfigViewController : ControllerBase<IConfigView>, IController<IConfigView>
    {
        protected override IConfigView ResolveView()
        {
            return new FormUTorrentConfiguration();
        }

        UTorrentAdapter _adapter;
        public UTorrentConfigViewController(UTorrentAdapter adapter)
        {
            _adapter = adapter;
            adapter.ConfigUpdated += new EventHandler<SharedLib.DataChangedEventArgs<IConfigModel>>(plugin_ConfigUpdated);
            View.ConfigChanging += new EventHandler<SharedLib.DataChangedEventArgs<IConfigModel>>(View_ConfigChanging);
            View.ConfigTesting += new EventHandler<SharedLib.GenericEventArgs<IConfigModel>>(View_ConfigTesting);
            if (View is IFormView)
            {
                ((IFormView)View).ApplyLanguage(TracyFacade.Instance.LanguageManager);
            }
        }

        void View_ConfigTesting(object sender, SharedLib.GenericEventArgs<IConfigModel> e)
        {
            try
            {
                _adapter.TestConfig(e.Data);
                MessageBox.Show(TracyFacade.Instance.LanguageManager.GetText("uTorrentAdapter_MessageBox_ConnectionTestPassed_Message"));
            }
            catch (Exception ex)
            {
                MessageBox.Show(TracyFacade.Instance.LanguageManager.GetText("uTorrentAdapter_MessageBox_ConnectionTestFailed_Message") + ex.Message);
            }
        }

        void View_ConfigChanging(object sender, SharedLib.DataChangedEventArgs<IConfigModel> e)
        {
            _adapter.UpdateConfig(e.OriginalData, e.Data);
        }

        void plugin_ConfigUpdated(object sender, SharedLib.DataChangedEventArgs<IConfigModel> e)
        {
            View.LoadConfig(e.Data);
        }


    }
}
